<?php
namespace Home\Controller;
use Think\Controller;
class PurchaseController extends Controller {
    public function _initialize()
    {
        //自动加载的类
        new \Org\Autoload();

        $this->member_id = $this->member_info['member_id'];
        //echo $member_id;exit;
    }
    //首页
    public function index($page=1,$sort=''){
        if(session('member_id')){
            $member_id=session('member_id');
            $member_name=session('member_name');
        }
        $data['title']='采购列表 - 找金银网';
        $data['active']='index';
        //$url=array();
        $base_url='/purchase/index';
        $Model=D("Purchase");
        //参数  页数
        if(intval($page)==0){
            $page=1;
        }else{
            $page=intval($page);
        }
        //排序
        if($sort!=''){
            $sort=addslashes($sort);
            if($sort=='price-asc'){
                $supply_sort='supply_price asc';
                $base_url.='/sort/'.$sort;
            }
            if($sort=='price-desc'){
                $supply_sort='supply_price desc';
                $base_url.='/sort/'.$sort;
            }
            if($sort=='time-asc'){
                $supply_sort='supply_time asc';
                $base_url.='/sort/'.$sort;
            }
            if($sort=='time-desc'){
                $supply_sort='supply_time desc';
                $base_url.='/sort/'.$sort;
            }
            //echo $sort;exit;
            /*if($sort=='sell_num-desc'){
                $search_sort='product_num desc';
            }
            if($sort=='sell_num-asc'){
                $search_sort='product_num asc';
            }
            if($sort=='price-desc'){
                $search_sort='product_price desc';
            }
            if($sort=='price-asc'){
                $search_sort='product_price asc';
            }
            if($sort=='add_time-desc'){
                $search_sort='product_time desc';
            }
            if($sort=='add_time-asc'){
                $search_sort='product_time asc';
            }*/
            $Model->supply_sort=$supply_sort;
        }
        $perpage=10;
        $Model->page=$page;
        $Model->perpage=$perpage;
        //echo $member_id;exit;
        $Model->member_id=$member_id;
        $purchase_list=$Model->get_purchase_list();
        //print_r($supply_list);exit;
        $purchase_total=$Model->get_purchase_total();
        //echo $supply_total;exit;
        $page_total=ceil($purchase_total/$perpage);
        //echo $page_total;exit;
        //分页数据
        if($page!=1&&$page_total>1){
            $last_page=$page-1;
        }else{
            $last_page=1;
        }
        if($page_total>$page){
            $next_page=$page+1;
        }else{
            $next_page=$page;
        }
        $pager['current_page']=$page;
        $pager['perpage']=$perpage;
        $pager['last_page']=$last_page;
        $pager['next_page']=$next_page;
        $pager['purchase_total']=$purchase_total;
        $pager['page_total']=$page_total;
        //$cat_list=$Model->get_cat_list();
        $this->assign("purchase_list", $purchase_list);
        //$this->assign("cat_list", $cat_list);
        $this->assign("pager",$pager);
        $this->assign("data", $data);
        $this->assign("base_url",$base_url);
//        $this->assign("sort_info",$sort_info);
        $this->assign("member_id", $member_id);
        $this->assign("member_name", $member_name);
        $this->display();
    }
    //获取AJAX数据
    public function get_ajax_data(){
        if(session('member_id')==''){
            $data['msg']='没有登录';echo json_encode($data);exit;
        }else{
            $purchase_id=I('post.purchase_id');
            $Model=D("Purchase");
            $member_id=$Model->where('purchase_id='.$purchase_id)->getField('member_id');
            //$data=$member_id;
            if($member_id==session('member_id')||$member_id==cookie('member_id')){
                $data['msg']='您不可以与自己进行交易';echo json_encode($data);exit;
            }
            //$data=array();
            $Model->purchase_id=$purchase_id;
            $data['info']=$Model->get_ajax_info();
            $data['msg']='ok';
            echo json_encode($data);exit;
        }
    }
    //提交AJAX数据
    public function submit_ajax_data(){
        if(session('member_id')==''){
            $data['msg']='没有登录';
        }else{
            $member_id=I('post.member_id');
            $purchase_id=I('post.purchase_id');
            $supply_num=I('post.supply_num');
            $supply_desc=I('post.supply_desc','','htmlspecialchars');
            $data=array();
            $Purchase=D("Purchase");
            $Model=D("Supply");
            $Match=D("Match");
//            $Order=D("Order");
            $Intention=D("Intention");
            $ProductClass=D("ProductClass");
            $Purchase->purchase_id=$purchase_id;
            $purchase_info=$Purchase->get_purchase_info();
            $post['supply_sn']=date('YmdHis',time()).rand(001,999);
            $post['member_id']=$member_id;
            $post['pc_id']=$purchase_info['pc_id'];
            $post['brand_id']=$purchase_info['brand_id'];
            $post['spec_id']=$purchase_info['spec_id'];
            $post['supply_image']=$purchase_info['purchase_image'];
            $post['supply_price']=$purchase_info['purchase_price'];
            $post['match_price']=$purchase_info['purchase_price'];
            $post['unit_id']=$purchase_info['unit_id'];
            $post['supply_num']=$supply_num;
            $post['match_num']=$supply_num;
            $post['depot_id']=$purchase_info['depot_id'];
            $post['deliver_time_id']=$purchase_info['deliver_time_id'];
            $post['deliver_type_id']=$purchase_info['deliver_type_id'];
            $post['shipping_type_id']=$purchase_info['shipping_type_id'];
            $post['supply_desc']=$supply_desc;
            $post['supply_time']=time();

            //写入供货表
            $Model->post=$post;
            if(!$Model->supply_add_post()){
                $data['msg']='写入数据库失败,请您重新提交';exit;
            }
            $supply_id=$Model->where('member_id='.$member_id)->order('supply_time desc')->getField('supply_id');
            $post['match_sn']=date('YmdHis',time()).rand(001,999);
            $post['supply_id']=$supply_id;
            $post['purchase_id']=$purchase_id;
            $post['match_desc']='用户自行操作生成的撮合记录';
            $post['match_time']=time();
            //print_r($post);exit;

            //写入撮合表
            $Match->post=$post;
            if(!$Match->match_add_post()){
                $data['msg']='写入数据库失败,请您重新提交';exit;
            }
            $match_id=$Match->where("match_sn='".$post['match_sn']."'")->order('match_time desc')->getField('match_id');
            
            //写入意向表
            $pc_id=$Model->where('member_id='.$member_id)->order('supply_time desc')->getField('pc_id');
            $pc_id=intval($pc_id);
            $post['intention_sort']=$ProductClass->where('pc_id='.$pc_id)->getField('pc_name');
            $post['intention_type']=1;
            $post['intention_num']=$supply_num;
            $post['intention_message']=$supply_desc;
            $post['intention_price']=$purchase_info['purchase_price'];
            $post['match_id']=$match_id;
            $Intention->post=$post;
            if(!$Intention->intention_add_post()){
                $data['msg']='写入数据库失败,请您重新提交';exit;
            }
            //写入订单表
//            $data['order_sn']=date('YmdHis').rand(001,999);
//            $data['match_id']=$match_id;
//            $data['seller_id']=$supply_info['member_id'];;
//            $data['buyer_id']=$member_id;
//            $data['goods_amount']=$supply_info['supply_price']*$purchase_num;
//            $data['order_amount']=$supply_info['supply_price']*$purchase_num;
//            $data['order_type']=2;  //1为商品订单2为撮合订单
//            $data['order_state_id']=1;  //状态ID 1为待付款
//            $data['order_time']=time();
            //print_r($data);exit;
//            $Order->data=$data;
//            if(!$Order->check_add_order()){
//                $data['msg']='写入数据库失败,请您重新提交';exit;
//            }
            $data['msg']='ok';
        }
        //$data=$supply_desc;
        echo json_encode($data);
    }
    public function search(){
        $data['title']='现货搜索 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    public function details(){
        $data['title']='现货详情 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    /*public function company(){
        $data['title']='资源列表 找金银网';
        $this->assign("data", $data);
        $this->display();
    }
    public function company_del(){
        $data['title']='资源列表 找金银网';
        $this->assign("data", $data);
        $this->display();
    }*/
}